package com.lap.context.starter.autoconfigure;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.lap.context.starter.autoconfigure.properties.ContextProperties;
import com.lap.context.starter.interceptor.RequestIdInterceptor;
import com.lap.context.starter.interceptor.UserContextInterceptor;
import lombok.extern.slf4j.Slf4j;

/**
 * 自动装载上下文
 *
 * @author Shuisheng Lao(劳水生)
 * @version 1.0.0
 */
@Slf4j
@AutoConfiguration
@EnableConfigurationProperties(ContextProperties.class)
public class ContextAutoConfiguration implements WebMvcConfigurer {

  @Bean
  HandlerInterceptor userContextInterceptor() {
    log.info("Initializer context userContextInterceptor");
    return new UserContextInterceptor();
  }

  @Bean
  RequestIdInterceptor requestIdInterceptor() {
    log.info("Initializer context RequestId Interceptor");
    return new RequestIdInterceptor();
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(userContextInterceptor());
    registry.addInterceptor(requestIdInterceptor());
  }
}
